

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="z">
  <meta name="keywords" content="java,集成redis">
  
    <meta name="description" content="1、pom依赖123456789101112&lt;!--  springboot整合redis --&gt;&lt;dependency&gt;	&lt;groupId&gt;org.springframework.boot&lt;&#x2F;groupId&gt;	&lt;artifactId&gt;spring-boot-starter-redis&lt;&#x2F;artifactId&gt;	&lt;ver">
<meta property="og:type" content="article">
<meta property="og:title" content="集成redis">
<meta property="og:url" content="https://hexo.xy94.top/2022/%E9%9B%86%E6%88%90redis.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="1、pom依赖123456789101112&lt;!--  springboot整合redis --&gt;&lt;dependency&gt;	&lt;groupId&gt;org.springframework.boot&lt;&#x2F;groupId&gt;	&lt;artifactId&gt;spring-boot-starter-redis&lt;&#x2F;artifactId&gt;	&lt;ver">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-09-22T06:46:36.000Z">
<meta property="article:modified_time" content="2024-02-02T01:41:48.499Z">
<meta property="article:author" content="z">
<meta property="article:tag" content="java">
<meta property="article:tag" content="集成redis">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>集成redis - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"hexo.xy94.top","root":"/","version":"1.9.7","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>个人分享</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/" target="_self">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/" target="_self">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/" target="_self">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/" target="_self">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/" target="_self">
                <i class="iconfont icon-user-fill"></i>
                <span>联系我</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/" target="_self">
                <i class="iconfont icon-link-fill"></i>
                <span>友链</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" target="_self" href="javascript:;" role="button"
                 data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="iconfont icon-books"></i>
                <span>文档</span>
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                
                  
                  
                  
                  <a class="dropdown-item" href="https://hexo.fluid-dev.com/" target="_self">
                    
                    <span>主题博客</span>
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" href="https://hexo.fluid-dev.com/docs/guide/" target="_self">
                    
                    <span>配置指南</span>
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" href="https://hexo.fluid-dev.com/docs/icon/" target="_self">
                    
                    <span>图标用法</span>
                  </a>
                
              </div>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="集成redis"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        z
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-09-22 14:46" pubdate>
          2022-09-22 02:46:36 星期四 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          <!-- compatible with older versions-->
          2.3k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          <!-- compatible with older versions-->
          19 分钟
        
      </span>
    

    
    
      
        <span id="leancloud-page-views-container" class="post-meta" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="leancloud-page-views"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>
<meta name="baidu-site-verification" content="codeva-jcIBX7fHxH" />
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">集成redis</h1>
            
            
              <div class="markdown-body">
                
                <h1 id="1、pom依赖"><a href="#1、pom依赖" class="headerlink" title="1、pom依赖"></a>1、pom依赖</h1><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--  springboot整合redis --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-redis<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.4.7.RELEASE<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>            <br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!--使用jedis及其连接池--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>redis.clients<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>jedis<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.9.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>
<h1 id="2、单机配置"><a href="#2、单机配置" class="headerlink" title="2、单机配置"></a>2、单机配置</h1><p>application.yml</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs yml"><span class="hljs-string">spring.redis.host=localhost</span><br><span class="hljs-string">spring.redis.port=6379</span><br><span class="hljs-string">spring.redis.password=123456</span><br><span class="hljs-comment">#连接池的最大连接数，使用负数没有限制</span><br><span class="hljs-string">spring.redis.jedis.pool.max-active=100</span><br><span class="hljs-comment">#连接池的最大空闲连接</span><br><span class="hljs-string">spring.redis.jedis.pool.max-idle=10</span><br><span class="hljs-comment">#连接池的最大阻塞等待时间</span><br><span class="hljs-string">spring.redis.jedis.pool.max-wait=100000</span><br><span class="hljs-comment"># 连接池中的最小空闲连接</span><br><span class="hljs-string">spring.redis.jedis.pool.min-idle=0</span><br><span class="hljs-comment"># 连接超时时间（毫秒）</span><br><span class="hljs-string">spring.redis.timeout=10000</span><br></code></pre></td></tr></table></figure>
<p>RedisConfig.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br><span class="line">425</span><br><span class="line">426</span><br><span class="line">427</span><br><span class="line">428</span><br><span class="line">429</span><br><span class="line">430</span><br><span class="line">431</span><br><span class="line">432</span><br><span class="line">433</span><br><span class="line">434</span><br><span class="line">435</span><br><span class="line">436</span><br><span class="line">437</span><br><span class="line">438</span><br><span class="line">439</span><br><span class="line">440</span><br><span class="line">441</span><br><span class="line">442</span><br><span class="line">443</span><br><span class="line">444</span><br><span class="line">445</span><br><span class="line">446</span><br><span class="line">447</span><br><span class="line">448</span><br><span class="line">449</span><br><span class="line">450</span><br><span class="line">451</span><br><span class="line">452</span><br><span class="line">453</span><br><span class="line">454</span><br><span class="line">455</span><br><span class="line">456</span><br><span class="line">457</span><br><span class="line">458</span><br><span class="line">459</span><br><span class="line">460</span><br><span class="line">461</span><br><span class="line">462</span><br><span class="line">463</span><br><span class="line">464</span><br><span class="line">465</span><br><span class="line">466</span><br><span class="line">467</span><br><span class="line">468</span><br><span class="line">469</span><br><span class="line">470</span><br><span class="line">471</span><br><span class="line">472</span><br><span class="line">473</span><br><span class="line">474</span><br><span class="line">475</span><br><span class="line">476</span><br><span class="line">477</span><br><span class="line">478</span><br><span class="line">479</span><br><span class="line">480</span><br><span class="line">481</span><br><span class="line">482</span><br><span class="line">483</span><br><span class="line">484</span><br><span class="line">485</span><br><span class="line">486</span><br><span class="line">487</span><br><span class="line">488</span><br><span class="line">489</span><br><span class="line">490</span><br><span class="line">491</span><br><span class="line">492</span><br><span class="line">493</span><br><span class="line">494</span><br><span class="line">495</span><br><span class="line">496</span><br><span class="line">497</span><br><span class="line">498</span><br><span class="line">499</span><br><span class="line">500</span><br><span class="line">501</span><br><span class="line">502</span><br><span class="line">503</span><br><span class="line">504</span><br><span class="line">505</span><br><span class="line">506</span><br><span class="line">507</span><br><span class="line">508</span><br><span class="line">509</span><br><span class="line">510</span><br><span class="line">511</span><br><span class="line">512</span><br><span class="line">513</span><br><span class="line">514</span><br><span class="line">515</span><br><span class="line">516</span><br><span class="line">517</span><br><span class="line">518</span><br><span class="line">519</span><br><span class="line">520</span><br><span class="line">521</span><br><span class="line">522</span><br><span class="line">523</span><br><span class="line">524</span><br><span class="line">525</span><br><span class="line">526</span><br><span class="line">527</span><br><span class="line">528</span><br><span class="line">529</span><br><span class="line">530</span><br><span class="line">531</span><br><span class="line">532</span><br><span class="line">533</span><br><span class="line">534</span><br><span class="line">535</span><br><span class="line">536</span><br><span class="line">537</span><br><span class="line">538</span><br><span class="line">539</span><br><span class="line">540</span><br><span class="line">541</span><br><span class="line">542</span><br><span class="line">543</span><br><span class="line">544</span><br><span class="line">545</span><br><span class="line">546</span><br><span class="line">547</span><br><span class="line">548</span><br><span class="line">549</span><br><span class="line">550</span><br><span class="line">551</span><br><span class="line">552</span><br><span class="line">553</span><br><span class="line">554</span><br><span class="line">555</span><br><span class="line">556</span><br><span class="line">557</span><br><span class="line">558</span><br><span class="line">559</span><br><span class="line">560</span><br><span class="line">561</span><br><span class="line">562</span><br><span class="line">563</span><br><span class="line">564</span><br><span class="line">565</span><br><span class="line">566</span><br><span class="line">567</span><br><span class="line">568</span><br><span class="line">569</span><br><span class="line">570</span><br><span class="line">571</span><br><span class="line">572</span><br><span class="line">573</span><br><span class="line">574</span><br><span class="line">575</span><br><span class="line">576</span><br><span class="line">577</span><br><span class="line">578</span><br><span class="line">579</span><br><span class="line">580</span><br><span class="line">581</span><br><span class="line">582</span><br><span class="line">583</span><br><span class="line">584</span><br><span class="line">585</span><br><span class="line">586</span><br><span class="line">587</span><br><span class="line">588</span><br><span class="line">589</span><br><span class="line">590</span><br><span class="line">591</span><br><span class="line">592</span><br><span class="line">593</span><br><span class="line">594</span><br><span class="line">595</span><br><span class="line">596</span><br><span class="line">597</span><br><span class="line">598</span><br><span class="line">599</span><br><span class="line">600</span><br><span class="line">601</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> com.fasterxml.jackson.annotation.JsonAutoDetect;<br><span class="hljs-keyword">import</span> com.fasterxml.jackson.annotation.PropertyAccessor;<br><span class="hljs-keyword">import</span> com.fasterxml.jackson.databind.ObjectMapper;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Value;<br><span class="hljs-keyword">import</span> org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;<br><span class="hljs-keyword">import</span> org.springframework.boot.autoconfigure.data.redis.RedisProperties;<br><span class="hljs-keyword">import</span> org.springframework.boot.context.properties.EnableConfigurationProperties;<br><span class="hljs-keyword">import</span> org.springframework.cache.annotation.CachingConfigurerSupport;<br><span class="hljs-keyword">import</span> org.springframework.cache.interceptor.KeyGenerator;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Bean;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Configuration;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.cache.RedisCacheConfiguration;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.cache.RedisCacheManager;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.connection.RedisStandaloneConfiguration;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.connection.jedis.JedisConnectionFactory;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.core.RedisTemplate;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.serializer.RedisSerializationContext;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.serializer.StringRedisSerializer;<br><br><span class="hljs-keyword">import</span> java.lang.reflect.Method;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 单机版 Redis缓存配置类，如需启用请在nacos中配置：jddods.redis.enabled=true</span><br><span class="hljs-comment">    */</span><br><span class="hljs-meta">@Configuration</span><br><span class="hljs-meta">@EnableConfigurationProperties(&#123;RedisProperties.class&#125;)</span><br><span class="hljs-meta">@ConditionalOnProperty(prefix = &quot;jddods.redis&quot;, name = &quot;enabled&quot;, havingValue = &quot;true&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">RedisConfig</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">CachingConfigurerSupport</span> &#123;<br>    <span class="hljs-meta">@Value(&quot;$&#123;spring.redis.host&#125;&quot;)</span><br>    <span class="hljs-keyword">private</span> String host;<br>    <span class="hljs-meta">@Value(&quot;$&#123;spring.redis.password&#125;&quot;)</span><br>    <span class="hljs-keyword">private</span> String password;<br>    <span class="hljs-meta">@Value(&quot;$&#123;spring.redis.port&#125;&quot;)</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> port;<br>    <span class="hljs-comment">//只有单机版支持此配置</span><br>    <span class="hljs-meta">@Value(&quot;$&#123;spring.redis.database:0&#125;&quot;)</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> database;<br><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> JedisConnectionFactory <span class="hljs-title function_">jedisConnectionFactory</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-type">RedisStandaloneConfiguration</span> <span class="hljs-variable">redisStandaloneConfiguration</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RedisStandaloneConfiguration</span>();<br>        redisStandaloneConfiguration.setHostName(host);<br>        redisStandaloneConfiguration.setPort(port);<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">JedisConnectionFactory</span>(redisStandaloneConfiguration);<br>    &#125;<br><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span> 自定义策略生成的key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@description</span> 自定义的缓存key的生成策略 若想使用这个key</span><br><span class="hljs-comment">        * 只需要讲注解上keyGenerator的值设置为keyGenerator即可&lt;/br&gt;</span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> KeyGenerator <span class="hljs-title function_">keyGenerator</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">KeyGenerator</span>() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">generate</span><span class="hljs-params">(Object target, Method method, Object... params)</span> &#123;<br>                <span class="hljs-type">StringBuilder</span> <span class="hljs-variable">sb</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringBuilder</span>();<br>                sb.append(target.getClass().getName());<br>                sb.append(method.getName());<br>                <span class="hljs-keyword">for</span> (Object obj : params) &#123;<br>                    sb.append(obj.toString());<br>                &#125;<br>                <span class="hljs-keyword">return</span> sb.toString();<br>            &#125;<br>        &#125;;<br>    &#125;<br><br>    <span class="hljs-comment">// 缓存管理器</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> RedisCacheManager <span class="hljs-title function_">cacheManager</span><span class="hljs-params">(JedisConnectionFactory jedisConnectionFactory)</span> &#123;<br>        <span class="hljs-comment">// 配置序列化（解决乱码的问题）</span><br>        <span class="hljs-type">StringRedisSerializer</span> <span class="hljs-variable">stringRedisSerializer</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringRedisSerializer</span>();<br><br>        <span class="hljs-type">Jackson2JsonRedisSerializer</span> <span class="hljs-variable">jackson2JsonRedisSerializer</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Jackson2JsonRedisSerializer</span>(Object.class);<br>        <span class="hljs-type">ObjectMapper</span> <span class="hljs-variable">om</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ObjectMapper</span>();<br>        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);<br>        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);<br>        jackson2JsonRedisSerializer.setObjectMapper(om);<br><br>        <span class="hljs-type">RedisCacheConfiguration</span> <span class="hljs-variable">config</span> <span class="hljs-operator">=</span> RedisCacheConfiguration.defaultCacheConfig()<br>                <span class="hljs-comment">// 默认 30天过期</span><br>                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer))<br>                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))<br>                .disableCachingNullValues();<br><br>        <span class="hljs-type">RedisCacheManager</span> <span class="hljs-variable">cacheManager</span> <span class="hljs-operator">=</span> RedisCacheManager<br>                .builder(jedisConnectionFactory)<br>                .cacheDefaults(config)<br>                .build();<br><br>        <span class="hljs-keyword">return</span> cacheManager;<br>    &#125;<br><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * RedisTemplate配置</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> jedisConnectionFactory</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> RedisTemplate&lt;String, Object&gt; <span class="hljs-title function_">redisTemplate</span><span class="hljs-params">(JedisConnectionFactory jedisConnectionFactory)</span> &#123;<br>        <span class="hljs-comment">// 设置序列化</span><br>        <span class="hljs-type">Jackson2JsonRedisSerializer</span> <span class="hljs-variable">jackson2JsonRedisSerializer</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Jackson2JsonRedisSerializer</span>(Object.class);<br>        <span class="hljs-type">ObjectMapper</span> <span class="hljs-variable">om</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ObjectMapper</span>();<br>        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);<br>        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);<br>        jackson2JsonRedisSerializer.setObjectMapper(om);<br>        <span class="hljs-comment">// 配置redisTemplate</span><br>        RedisTemplate&lt;String, Object&gt; redisTemplate = <span class="hljs-keyword">new</span> <span class="hljs-title class_">RedisTemplate</span>&lt;String, Object&gt;();<br>        redisTemplate.setConnectionFactory(jedisConnectionFactory);<br>        <span class="hljs-type">StringRedisSerializer</span> <span class="hljs-variable">stringSerializer</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringRedisSerializer</span>();<br><br>        redisTemplate.setKeySerializer(stringSerializer);<span class="hljs-comment">// key序列化</span><br>        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);<span class="hljs-comment">// value序列化</span><br><br>        redisTemplate.setHashKeySerializer(stringSerializer);<span class="hljs-comment">// Hash key序列化</span><br>        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);<span class="hljs-comment">// Hash value序列化</span><br>        redisTemplate.afterPropertiesSet();<br>        <span class="hljs-keyword">return</span> redisTemplate;<br>    &#125;<br><br>&#125;<br><br><br># <span class="hljs-number">3</span>、单机工具类<br><br><br><br><span class="hljs-keyword">import</span> cn.hutool.core.util.ObjectUtil;<br><span class="hljs-keyword">import</span> org.slf4j.Logger;<br><span class="hljs-keyword">import</span> org.slf4j.LoggerFactory;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.core.*;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.support.atomic.RedisAtomicLong;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Service;<br><br><span class="hljs-keyword">import</span> java.io.Serializable;<br><span class="hljs-keyword">import</span> java.util.*;<br><span class="hljs-keyword">import</span> java.util.concurrent.TimeUnit;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * Created by wjd on 2019/5/7.</span><br><span class="hljs-comment">    */</span><br><span class="hljs-meta">@Service</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">RedisService</span> &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-type">Logger</span> <span class="hljs-variable">logger</span> <span class="hljs-operator">=</span> LoggerFactory.getLogger(<span class="hljs-built_in">this</span>.getClass());<br><br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> RedisTemplate redisTemplate;<br><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 模糊查询key</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> Set&lt;String&gt; <span class="hljs-title function_">listKeys</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key)</span> &#123;<br>        Set&lt;String&gt; keys = redisTemplate.keys(key);<br>        <span class="hljs-keyword">return</span> keys;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 重命名</span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">rename</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String oldKey, <span class="hljs-keyword">final</span> String newKey)</span> &#123;<br>        redisTemplate.rename(oldKey, newKey);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 模糊获取</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> pattern</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> List&lt;Object&gt; <span class="hljs-title function_">listPattern</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String pattern)</span> &#123;<br>        List&lt;Object&gt; result = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        Set&lt;Serializable&gt; keys = redisTemplate.keys(pattern);<br>        <span class="hljs-keyword">for</span> (Serializable str : keys) &#123;<br>            ValueOperations&lt;Serializable, Object&gt; operations = redisTemplate.opsForValue();<br>            <span class="hljs-type">Object</span> <span class="hljs-variable">obj</span> <span class="hljs-operator">=</span> operations.get(str.toString());<br>            <span class="hljs-keyword">if</span> (!ObjectUtil.isEmpty(obj)) &#123;<br>                result.add(obj);<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">return</span> result;<br>    &#125;<br><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 写入缓存</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> value</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">set</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, Object value)</span> &#123;<br>        <span class="hljs-type">boolean</span> <span class="hljs-variable">result</span> <span class="hljs-operator">=</span> <span class="hljs-literal">false</span>;<br>        <span class="hljs-keyword">try</span> &#123;<br>            ValueOperations&lt;Serializable, Object&gt; operations = redisTemplate.opsForValue();<br>            operations.set(key, value);<br>            result = <span class="hljs-literal">true</span>;<br>        &#125; <span class="hljs-keyword">catch</span> (Exception e) &#123;<br>            logger.error(<span class="hljs-string">&quot;set fail ,key is:&quot;</span> + key, e);<br>        &#125;<br>        <span class="hljs-keyword">return</span> result;<br>    &#125;<br><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 批量写入缓存</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> map</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">multiSet</span><span class="hljs-params">(Map&lt;String, Object&gt; map)</span> &#123;<br>        <span class="hljs-type">boolean</span> <span class="hljs-variable">result</span> <span class="hljs-operator">=</span> <span class="hljs-literal">false</span>;<br>        <span class="hljs-keyword">try</span> &#123;<br>            ValueOperations&lt;Serializable, Object&gt; operations = redisTemplate.opsForValue();<br>            operations.multiSet(map);<br>            result = <span class="hljs-literal">true</span>;<br>        &#125; <span class="hljs-keyword">catch</span> (Exception e) &#123;<br>            logger.error(<span class="hljs-string">&quot;multiSet fail &quot;</span>, e);<br>        &#125;<br>        <span class="hljs-keyword">return</span> result;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 集合出栈</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">leftPop</span><span class="hljs-params">(String key)</span> &#123;<br>        <span class="hljs-type">ListOperations</span> <span class="hljs-variable">list</span> <span class="hljs-operator">=</span> redisTemplate.opsForList();<br>        <span class="hljs-keyword">return</span> list.leftPop(key);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">llen</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key)</span> &#123;<br>        <span class="hljs-keyword">final</span> <span class="hljs-type">ListOperations</span> <span class="hljs-variable">list</span> <span class="hljs-operator">=</span> <span class="hljs-built_in">this</span>.redisTemplate.opsForList();<br>        <span class="hljs-keyword">return</span> list.size((Object) key);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 写入缓存设置时效时间</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> value</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">set</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, Object value, Long expireTime)</span> &#123;<br>        <span class="hljs-type">boolean</span> <span class="hljs-variable">result</span> <span class="hljs-operator">=</span> <span class="hljs-literal">false</span>;<br>        <span class="hljs-keyword">try</span> &#123;<br>            ValueOperations&lt;Serializable, Object&gt; operations = redisTemplate.opsForValue();<br>            operations.set(key, value);<br>            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);<br>            result = <span class="hljs-literal">true</span>;<br>        &#125; <span class="hljs-keyword">catch</span> (Exception e) &#123;<br>            logger.error(<span class="hljs-string">&quot;set fail &quot;</span>, e);<br>        &#125;<br>        <span class="hljs-keyword">return</span> result;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 写入缓存设置时效时间</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> value</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">setnx</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, Object value, Long expireTime)</span> &#123;<br>        <span class="hljs-type">boolean</span> <span class="hljs-variable">res</span> <span class="hljs-operator">=</span> <span class="hljs-literal">false</span>;<br>        <span class="hljs-keyword">try</span> &#123;<br>            ValueOperations&lt;Serializable, Object&gt; operations = redisTemplate.opsForValue();<br>            res = operations.setIfAbsent(key, value);<br>            <span class="hljs-keyword">if</span> (res) &#123;<br>                redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);<br>            &#125;<br>        &#125; <span class="hljs-keyword">catch</span> (Exception e) &#123;<br>            logger.error(<span class="hljs-string">&quot;setnx fail &quot;</span>, e);<br>        &#125;<br>        <span class="hljs-keyword">return</span> res;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 缓存设置时效时间</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> expireTime</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">expire</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, Long expireTime)</span> &#123;<br>        redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);<br>    &#125;<br><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 自增操作</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">long</span> <span class="hljs-title function_">incr</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key)</span> &#123;<br>        <span class="hljs-type">RedisAtomicLong</span> <span class="hljs-variable">entityIdCounter</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RedisAtomicLong</span>(key, redisTemplate.getConnectionFactory());<br>        <span class="hljs-keyword">return</span> entityIdCounter.getAndIncrement();<br><br>    &#125;<br><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 批量删除</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> keys</span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">removeKeys</span><span class="hljs-params">(<span class="hljs-keyword">final</span> List&lt;String&gt; keys)</span> &#123;<br>        <span class="hljs-keyword">if</span> (keys.size() &gt; <span class="hljs-number">0</span>) &#123;<br>            redisTemplate.delete(keys);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 批量删除key</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> pattern</span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">removePattern</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String pattern)</span> &#123;<br>        Set&lt;Serializable&gt; keys = redisTemplate.keys(pattern);<br>        <span class="hljs-keyword">if</span> (keys.size() &gt; <span class="hljs-number">0</span>) &#123;<br>            redisTemplate.delete(keys);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 删除对应的value</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">remove</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key)</span> &#123;<br>        <span class="hljs-keyword">if</span> (exists(key)) &#123;<br>            redisTemplate.delete(key);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 判断缓存中是否有对应的value</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">exists</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key)</span> &#123;<br>        <span class="hljs-keyword">return</span> redisTemplate.hasKey(key);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 判断缓存中是否有对应的value(模糊匹配)</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> pattern</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">existsPattern</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String pattern)</span> &#123;<br>        <span class="hljs-keyword">if</span> (redisTemplate.keys(pattern).size() &gt; <span class="hljs-number">0</span>) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 读取缓存</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">get</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key)</span> &#123;<br>        ValueOperations&lt;Serializable, Object&gt; operations = redisTemplate.opsForValue();<br>        <span class="hljs-keyword">return</span> operations.get(key);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 哈希 添加</span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">hmSet</span><span class="hljs-params">(String key, Object hashKey, Object value)</span> &#123;<br>        HashOperations&lt;String, Object, Object&gt; hash = redisTemplate.opsForHash();<br>        hash.put(key, hashKey, value);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 哈希 添加</span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> Boolean <span class="hljs-title function_">hmSet</span><span class="hljs-params">(String key, Object hashKey, Object value, Long expireTime, TimeUnit timeUnit)</span> &#123;<br>        HashOperations&lt;String, Object, Object&gt; hash = redisTemplate.opsForHash();<br>        hash.put(key, hashKey, value);<br>        <span class="hljs-keyword">return</span> redisTemplate.expire(key, expireTime, timeUnit);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 哈希获取数据</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> hashKey</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">hmGet</span><span class="hljs-params">(String key, Object hashKey)</span> &#123;<br>        HashOperations&lt;String, Object, Object&gt; hash = redisTemplate.opsForHash();<br>        <span class="hljs-keyword">return</span> hash.get(key, hashKey);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 哈希获取所有数据</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">hmGetValues</span><span class="hljs-params">(String key)</span> &#123;<br>        HashOperations&lt;String, Object, Object&gt; hash = redisTemplate.opsForHash();<br>        <span class="hljs-keyword">return</span> hash.values(key);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 哈希获取所有键值</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">hmGetKeys</span><span class="hljs-params">(String key)</span> &#123;<br>        HashOperations&lt;String, Object, Object&gt; hash = redisTemplate.opsForHash();<br>        <span class="hljs-keyword">return</span> hash.keys(key);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 哈希获取所有键值对</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">hmGetMap</span><span class="hljs-params">(String key)</span> &#123;<br>        HashOperations&lt;String, Object, Object&gt; hash = redisTemplate.opsForHash();<br>        <span class="hljs-keyword">return</span> hash.entries(key);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 哈希 删除域</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> hashKey</span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> Long <span class="hljs-title function_">hdel</span><span class="hljs-params">(String key, Object hashKey)</span> &#123;<br>        HashOperations&lt;String, Object, Object&gt; hash = redisTemplate.opsForHash();<br>        <span class="hljs-keyword">return</span> hash.delete(key, hashKey);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 列表添加</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> k</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> v</span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">rPush</span><span class="hljs-params">(String k, Object v)</span> &#123;<br>        ListOperations&lt;String, Object&gt; list = redisTemplate.opsForList();<br>        list.rightPush(k, v);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 列表删除</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> k</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> v</span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">listRemove</span><span class="hljs-params">(String k, Object v)</span> &#123;<br>        ListOperations&lt;String, Object&gt; list = redisTemplate.opsForList();<br>        list.remove(k, <span class="hljs-number">1</span>, v);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">rPushAll</span><span class="hljs-params">(String k, Collection var2)</span> &#123;<br>        ListOperations&lt;String, Object&gt; list = redisTemplate.opsForList();<br>        list.rightPushAll(k, var2);<br>    &#125;<br><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 列表获取</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> k</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> begin</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> end</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">lRange</span><span class="hljs-params">(String k, <span class="hljs-type">long</span> begin, <span class="hljs-type">long</span> end)</span> &#123;<br>        ListOperations&lt;String, Object&gt; list = redisTemplate.opsForList();<br>        <span class="hljs-keyword">return</span> list.range(k, begin, end);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 集合添加</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> value</span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">add</span><span class="hljs-params">(String key, Object value)</span> &#123;<br>        SetOperations&lt;String, Object&gt; set = redisTemplate.opsForSet();<br>        set.add(key, value);<br>    &#125;<br><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 判断元素是否在集合中</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> value</span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> Boolean <span class="hljs-title function_">isMember</span><span class="hljs-params">(String key, Object value)</span> &#123;<br>        SetOperations&lt;String, Object&gt; set = redisTemplate.opsForSet();<br>        <span class="hljs-keyword">return</span> set.isMember(key, value);<br>    &#125;<br><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 集合获取</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> Set&lt;Object&gt; <span class="hljs-title function_">setMembers</span><span class="hljs-params">(String key)</span> &#123;<br>        SetOperations&lt;String, Object&gt; set = redisTemplate.opsForSet();<br>        <span class="hljs-keyword">return</span> set.members(key);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 有序集合添加</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> value</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> scoure</span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">zAdd</span><span class="hljs-params">(String key, Object value, <span class="hljs-type">double</span> scoure)</span> &#123;<br>        ZSetOperations&lt;String, Object&gt; zset = redisTemplate.opsForZSet();<br>        zset.add(key, value, scoure);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 有序集合获取</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> scoure</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> scoure1</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> Set&lt;Object&gt; <span class="hljs-title function_">rangeByScore</span><span class="hljs-params">(String key, <span class="hljs-type">double</span> scoure, <span class="hljs-type">double</span> scoure1)</span> &#123;<br>        ZSetOperations&lt;String, Object&gt; zset = redisTemplate.opsForZSet();<br>        <span class="hljs-keyword">return</span> zset.rangeByScore(key, scoure, scoure1);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 有序集合根据区间删除</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> scoure</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> scoure1</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">removeRangeByScore</span><span class="hljs-params">(String key, <span class="hljs-type">double</span> scoure, <span class="hljs-type">double</span> scoure1)</span> &#123;<br>        ZSetOperations&lt;String, Object&gt; zset = redisTemplate.opsForZSet();<br>        zset.removeRangeByScore(key, scoure, scoure1);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 列表添加</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> k</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> v</span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">lPush</span><span class="hljs-params">(String k, Object v)</span> &#123;<br>        ListOperations&lt;String, Object&gt; list = redisTemplate.opsForList();<br>        list.rightPush(k, v);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 获取当前key的超时时间</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@param</span> key</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-keyword">public</span> Long <span class="hljs-title function_">getExpireTime</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key)</span> &#123;<br>        <span class="hljs-keyword">return</span> redisTemplate.opsForValue().getOperations().getExpire(key, TimeUnit.SECONDS);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> Long <span class="hljs-title function_">extendExpireTime</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String key, Long extendTime)</span> &#123;<br>        <span class="hljs-type">Long</span> <span class="hljs-variable">curTime</span> <span class="hljs-operator">=</span> redisTemplate.opsForValue().getOperations().getExpire(key, TimeUnit.SECONDS);<br>        <span class="hljs-type">long</span> <span class="hljs-variable">total</span> <span class="hljs-operator">=</span> curTime.longValue() + extendTime;<br>        redisTemplate.expire(key, total, TimeUnit.SECONDS);<br>        <span class="hljs-keyword">return</span> total;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> Set <span class="hljs-title function_">getKeys</span><span class="hljs-params">(String k)</span> &#123;<br>        <span class="hljs-keyword">return</span> redisTemplate.keys(k);<br>    &#125;<br><br><br>&#125;<br></code></pre></td></tr></table></figure>
<h1 id="4、集群配置"><a href="#4、集群配置" class="headerlink" title="4、集群配置"></a>4、集群配置</h1><p>application.yml</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs yml"><span class="hljs-comment">#集群配置</span><br><span class="hljs-string">spring.redis.cluster.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384</span><br><span class="hljs-comment">## 连接超时时间（毫秒）</span><br><span class="hljs-string">spring.redis.timeout=60000</span><br><span class="hljs-comment">## Redis数据库索引(默认为0)</span><br><span class="hljs-string">spring.redis.database=0</span><br><span class="hljs-comment">#连接池的最大连接数，使用负数没有限制</span><br><span class="hljs-string">spring.redis.jedis.pool.max-active=100</span><br><span class="hljs-comment">#连接池的最大空闲连接</span><br><span class="hljs-string">spring.redis.jedis.pool.max-idle=10</span><br><span class="hljs-comment">#连接池的最大阻塞等待时间</span><br><span class="hljs-string">spring.redis.jedis.pool.max-wait=100000</span><br><span class="hljs-comment"># 连接池中的最小空闲连接</span><br><span class="hljs-string">spring.redis.jedis.pool.min-idle=0</span><br><span class="hljs-comment"># 连接超时时间（毫秒）</span><br><span class="hljs-string">spring.redis.timeout=10000</span><br></code></pre></td></tr></table></figure>

<p>ClusterRedisConfig.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> com.fasterxml.jackson.annotation.JsonAutoDetect;<br><span class="hljs-keyword">import</span> com.fasterxml.jackson.annotation.PropertyAccessor;<br><span class="hljs-keyword">import</span> com.fasterxml.jackson.databind.ObjectMapper;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Value;<br><span class="hljs-keyword">import</span> org.springframework.boot.autoconfigure.condition.ConditionalOnClass;<br><span class="hljs-keyword">import</span> org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Bean;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Configuration;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.connection.RedisConnectionFactory;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.core.RedisTemplate;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;<br><span class="hljs-keyword">import</span> org.springframework.data.redis.serializer.StringRedisSerializer;<br><span class="hljs-keyword">import</span> redis.clients.jedis.HostAndPort;<br><span class="hljs-keyword">import</span> redis.clients.jedis.JedisCluster;<br><span class="hljs-keyword">import</span> redis.clients.jedis.JedisPoolConfig;<br><br><span class="hljs-keyword">import</span> java.net.UnknownHostException;<br><span class="hljs-keyword">import</span> java.util.HashSet;<br><span class="hljs-keyword">import</span> java.util.Set;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 集群版 Redis缓存配置类，如需启用请在配置文件中新增：jddods.redis.cluster=true</span><br><span class="hljs-comment">    */</span><br><span class="hljs-meta">@Configuration</span><br><span class="hljs-meta">@ConditionalOnClass(&#123;JedisCluster.class&#125;)</span><br><span class="hljs-meta">@ConditionalOnProperty(prefix = &quot;jddods.redis&quot;, name = &quot;cluster&quot;, havingValue = &quot;true&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ClusterRedisConfig</span> &#123;<br><br>    <span class="hljs-meta">@Value(&quot;$&#123;spring.redis.cluster.nodes&#125;&quot;)</span><br>    <span class="hljs-keyword">private</span> String clusterNodes;<br>    <span class="hljs-meta">@Value(&quot;$&#123;spring.redis.timeout&#125;&quot;)</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> timeout;<br>    <span class="hljs-meta">@Value(&quot;$&#123;spring.redis.pool.max-idle&#125;&quot;)</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> maxIdle;<br>    <span class="hljs-meta">@Value(&quot;$&#123;spring.redis.pool.max-wait&#125;&quot;)</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-type">long</span> maxWaitMillis;<br>    <span class="hljs-meta">@Value(&quot;$&#123;spring.redis.commandTimeout&#125;&quot;)</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> commandTimeout;<br><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> JedisCluster <span class="hljs-title function_">getJedisCluster</span><span class="hljs-params">()</span> &#123;<br>        String[] cNodes = clusterNodes.split(<span class="hljs-string">&quot;,&quot;</span>);<br>        Set&lt;HostAndPort&gt; nodes = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashSet</span>&lt;&gt;();<br>        <span class="hljs-comment">//分割出集群节点</span><br>        <span class="hljs-keyword">for</span> (String node : cNodes) &#123;<br>            String[] hp = node.split(<span class="hljs-string">&quot;:&quot;</span>);<br>            nodes.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">HostAndPort</span>(hp[<span class="hljs-number">0</span>], Integer.parseInt(hp[<span class="hljs-number">1</span>])));<br>        &#125;<br>        <span class="hljs-type">JedisPoolConfig</span> <span class="hljs-variable">jedisPoolConfig</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">JedisPoolConfig</span>();<br>        jedisPoolConfig.setMaxIdle(maxIdle);<br>        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);<br>        <span class="hljs-comment">//创建集群对象</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">JedisCluster</span>(nodes, commandTimeout, jedisPoolConfig);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">        * 设置数据存入redis 的序列化方式</span><br><span class="hljs-comment">        * &lt;/br&gt;redisTemplate序列化默认使用的jdkSerializeable,存储二进制字节码,导致key会出现乱码，所以自定义</span><br><span class="hljs-comment">        * 序列化类</span><br><span class="hljs-comment">        *</span><br><span class="hljs-comment">        * <span class="hljs-doctag">@paramredisConnectionFactory</span></span><br><span class="hljs-comment">        */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> RedisTemplate&lt;Object, Object&gt; <span class="hljs-title function_">redisTemplate</span><span class="hljs-params">(RedisConnectionFactory redisConnectionFactory)</span> <span class="hljs-keyword">throws</span> UnknownHostException &#123;<br>        RedisTemplate&lt;Object, Object&gt; redisTemplate = <span class="hljs-keyword">new</span> <span class="hljs-title class_">RedisTemplate</span>&lt;&gt;();<br>        redisTemplate.setConnectionFactory(redisConnectionFactory);<br>        <span class="hljs-type">Jackson2JsonRedisSerializer</span> <span class="hljs-variable">jackson2JsonRedisSerializer</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Jackson2JsonRedisSerializer</span>(Object.class);<br>        <span class="hljs-type">ObjectMapper</span> <span class="hljs-variable">objectMapper</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ObjectMapper</span>();<br>        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);<br>        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);<br>        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);<br><br>        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);<br>        redisTemplate.setKeySerializer(<span class="hljs-keyword">new</span> <span class="hljs-title class_">StringRedisSerializer</span>());<br><br>        redisTemplate.afterPropertiesSet();<br><br>        <span class="hljs-keyword">return</span> redisTemplate;<br>    &#125;<br><br>&#125;<br></code></pre></td></tr></table></figure>
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/java/" class="category-chain-item">java</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/java/" class="print-no-link">#java</a>
      
        <a href="/tags/%E9%9B%86%E6%88%90redis/" class="print-no-link">#集成redis</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>集成redis</div>
      <div>https://hexo.xy94.top/2022/集成redis.html</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>z</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年9月22日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/git%E7%AE%80%E6%98%93%E7%9A%84%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%85%A5%E9%97%A8.html" title="git简易的命令行入门">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">git简易的命令行入门</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/jwt.html" title="jwt">
                        <span class="hidden-mobile">jwt</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="/js/leancloud.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
